package com.xxx.common.web.configure;

import com.xxx.common.web.interceptor.TenantIdInterceptor;
import com.xxx.common.web.interceptor.TokenInterceptor;
import com.xxx.common.web.interceptor.TraceIdInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 1. TokenInterceptor（最先执行）
        registry.addInterceptor(new TokenInterceptor())
                .order(Ordered.HIGHEST_PRECEDENCE) // 最高优先级
                .addPathPatterns("/**");

        // 2. LoggingInterceptor
        registry.addInterceptor(new TraceIdInterceptor())
                .order(Ordered.HIGHEST_PRECEDENCE + 10) // 次高优先级
                .addPathPatterns("/**");

        // 3. TenantIdInterceptor
        registry.addInterceptor(new TenantIdInterceptor())
                .order(Ordered.HIGHEST_PRECEDENCE + 20) // 次高优先级
                .addPathPatterns("/**");
    }
}